package chanfoofoo;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

// import org.hibernate.Session;

public class StockQuote_manager {
	private List<StockQuote> list_StockQuote = null ;
	private StringBuffer msg = new StringBuffer("START") ;   
	private User currentUser = null ; 
	
	public StockQuote_manager() {
		System.out.println(this.getClass().getName()) ;
		// userAction("LIST") ; 
	}
	public void list_all_to_console() {
		StockQuote tmp_stock ; 
//		Session session = util.HibernateUtil.getSessionFactory().getCurrentSession();
//		session.beginTransaction();		
		if (list_StockQuote != null) {
		for (int i = 0 ; i<list_StockQuote.size() ; i++) {
			tmp_stock = list_StockQuote.get(i) ; 
			System.out.println(tmp_stock.getId() + " " + tmp_stock.getStockcode()) ;
			// session.saveOrUpdate(tmp_stock);			
		}
		}
//		session.getTransaction().commit();
	}
	public void XXXuserAction(String in_action) {
		// list_StockQuote = (List<xxStockQuote>) pm.newQuery(query).execute();
		list_StockQuote = new ArrayList(5) ;
		
		for (int i=0 ; i<5 ; i++) {
			StockQuote tmp_stockquote = new StockQuote(String.valueOf(3030 + i)) ;
			list_StockQuote.add(tmp_stockquote) ; 
		}
		
	}
	
	
	public void userAction(String in_action) {
//		Session session = util.HibernateUtil.getSessionFactory().getCurrentSession();
		
		 
		PersistenceManager pm = PMF01.get().getPersistenceManager();
//		StockQuote stockquote = new StockQuote("6022") ; 
//        try {
//            pm.makePersistent(stockquote);
//        } finally {
//            // pm.close();
//        }
		if (in_action.equals("SAVE")) {
			  
			for (StockQuote tmp_stock : list_StockQuote) {
				System.out.println("SAVE : " + tmp_stock.getId() + " " + tmp_stock.getStockcode()) ;
				pm.makePersistent(tmp_stock) ;						
			}
			pm.close() ;
			pm = PMF01.get().getPersistenceManager();
		} 	
	
	    // String query = "select from " + StockQuote.class.getName()  ;
	    // list_StockQuote = (List<StockQuote>) pm.newQuery(query).execute();   
//		session.beginTransaction();
//		list_StockQuote = (List<StockQuote>) session.createQuery("from StockQuote").list();
//		session.getTransaction().commit();
		// PersistenceManager pm = PMF01.get().getPersistenceManager();
		
		String tmp_user_id = "" ; 
	    String query = "select from " + StockQuote.class.getName() ; 
	    if (currentUser != null) {
	    	tmp_user_id = currentUser.getUser_id() ;
	    	query = query + " where user_id == '" +  tmp_user_id + "'" ;
	    }
	    
	    // String query = "select from " + StockQuote.class.getName() ; 

	    List tmp_list = (List)pm.newQuery(query).execute();
	    list_StockQuote = (List<StockQuote>) pm.detachCopyAll(tmp_list); 
	    // list_StockQuote = (List<StockQuote>) pm.newQuery(query).execute();
	    if (currentUser != null) {
		    while (list_StockQuote.size()<5){
		    	StockQuote stockquote = new StockQuote("0000") ;
		    	stockquote.setUser_id(tmp_user_id) ; 
		    	list_StockQuote.add(stockquote) ; 
		    }
	    }
	    
		pm.close() ; 
	}
	
	public void action_update() {
		StockQuote tmp_stock = null ; 	
		for (int i = 0 ; i<list_StockQuote.size() ; i++) {
			tmp_stock = list_StockQuote.get(i) ; 
			msg.append(tmp_stock.getStockcode() ) ; 
		}
		System.out.println("UPDATE") ; // userAction("UPDATE"); 
	}
	
	public void setList_StockQuote(List<StockQuote> list_StockQuote) {
		this.list_StockQuote = list_StockQuote;
	}

	public List<StockQuote> getList_StockQuote() {
		return list_StockQuote;
		}


	public void setMsg(StringBuffer msg) {
		this.msg = msg;
	}

	public StringBuffer getMsg() {
		return msg;
	}
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
		userAction("LIST") ; 
	}
	public User getCurrentUser() {
		return currentUser;
	}	
}
